/*
 * Copyright 2002-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.deservel.wxopen.controller;

import ch.qos.logback.classic.Logger;
import com.deservel.wxopen.model.userinfo.SNSUserInfo;
import com.deservel.wxopen.model.userinfo.WeixinOauth2Token;
import com.deservel.wxopen.service.Impl.OAuth2ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @author DeserveL
 * @date 2017/7/10 15:38
 * @since 1.0.0
 */
@RestController
@RequestMapping("/oauth")
public class OAuth2Controller {

    @Autowired
    OAuth2ServiceImpl oAuth2Service;

    @RequestMapping("")
    public HashMap<String, Object> getOauth(String code, String statu) {
        HashMap<String, Object> rs = new HashMap<>();
        rs.put("flag", false);
        //用户同意授权
        if (!"authdeny".equals(code)) {
            WeixinOauth2Token oauth2AccessToken = oAuth2Service.getOauth2AccessToken(code);
            if (null != oAuth2Service) {
                SNSUserInfo snsUserInfo = oAuth2Service.getSNSUserInfo(oauth2AccessToken.getAccess_token(), oauth2AccessToken.getOpenid());
                rs.put("flag", true);
                rs.put("data", snsUserInfo);
            } else {
                rs.put("msg", "未知错误");
            }
        } else {
            rs.put("msg", "用户拒绝");
        }
        return rs;
    }
}
